<?php

header("Content-Type: text/json; charset=utf-8");
require_once("../../core/cfg.php");
require_once("../../core/functions.php");

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' : false)
{
	$new_sys_name = translite($_POST["new_sys_name"]);
	$db_table     = $_POST["db_table"];
	$id_item      = $_POST["id_item"];
	$flag         = $_POST["flag"];
	
	$separator = "_";	
	$new_sys_name = str_replace(array("!", "?", "*", "&quot;", "&#39;", "«", "»", "%", "@", "`", ",", ".", "-", "/", ")", "(", ":", '"', "#", "№"), $separator, $new_sys_name);
	$new_sys_name = preg_replace('/__+/', '_', $new_sys_name);
	$new_sys_name = trim($new_sys_name, $separator);
	
	$r =& Registry::get("MDB")->query("SELECT `ID` FROM `".$db_table."` WHERE `Sys_name`='".$new_sys_name."' LIMIT 1;");		
	if (PEAR::isError($r)) {
		die($r->getMessage().' ::: '.$r->getUserInfo());
	}	
	if ($r->numRows()) {
		$new_sys_name = functions::get_free_num($new_sys_name, $db_table,1);
	}			
	$json["sys_name"] = $new_sys_name;		
	exit(getJSON($json));
}

?>